/*
https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/solution/san-ge-wu-zhong-die-zi-shu-zu-de-zui-da-4a8lb/
 */
import java.util.Arrays;

public class Solution689 {
    public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
        int[] ans=new int[3];
        int sum1=0,maxSum1=0,maxSum1Idx=0;
        int sum2=0,maxSum12=0,maxSum12Idx1=0,maxSum12Idx2=0;
        int sum3=0,maxTotal=0;
        for (int i=k*2;i<nums.length;i++){
            sum1+=nums[i-k*2];
            sum2+=nums[i-k];
            sum3+=nums[i];
            if (i>=3*k-1){
                if (sum1>maxSum1){
                    maxSum1=sum1;
                    maxSum1Idx=i-k*3+1;
                }
                if (maxSum1 +sum2>maxSum12){
                    maxSum12=maxSum1+sum2;
                    maxSum12Idx1=maxSum1Idx;
                    maxSum12Idx2=i-k*2+1;
                }
                if (maxSum12+sum3>maxTotal){
                    maxTotal=maxSum12+sum3;
                    ans[0]=maxSum12Idx1;
                    ans[1]=maxSum12Idx2;
                    ans[2]=i-k+1;
                }
                sum1-=nums[i-k*3+1];
                sum2-=nums[i-k*2+1];
                sum3-=nums[i-k+1];
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution689().maxSumOfThreeSubarrays(new int[]{1,2,1,2,6,7,5,1},2)));
    }
}
